package cn.lysj.framework.coupon.service;

import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.utils.random.CustomTimeUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.coupon.domain.BirthdayCouponConfig;
import cn.lysj.framework.coupon.domain.Coupon;
import cn.lysj.framework.coupon.domain.NewVipCouponConfig;
import cn.lysj.framework.coupon.repository.NewVipCouponConfigRepository;
import cn.lysj.framework.coupon.vo.BirthdayCouponToUserVo;
import cn.lysj.framework.coupon.vo.NewVipCouponToStoreVo;
import cn.lysj.framework.coupon.vo.NewVipCouponToUserVo;
import cn.lysj.framework.customer.domain.CustomerInfo;
import cn.lysj.framework.customer.domain.CustomerVip;
import cn.lysj.framework.customer.service.CustomerInfoService;
import cn.lysj.framework.customer.service.CustomerVipCouponService;
import cn.lysj.framework.customer.service.CustomerVipService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zwj
 * @description 新加入会员优惠券配置服务层
 * @create 2019-01-16 09:37:56
 **/
@Slf4j
@Service
@Transactional
public class NewVipCouponConfigService implements BaseService<NewVipCouponConfig> {

    @Resource
    private NewVipCouponConfigRepository newVipCouponConfigRepository;

    @Resource
    private CustomerVipCouponService customerVipCouponService;

    @Resource
    private CouponService couponService;

    @Resource
    private CustomerVipService customerVipService;

    @Resource
    private CustomerInfoService customerInfoService;

    @Override
    public NewVipCouponConfigRepository getRepository() {
        return newVipCouponConfigRepository;
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/21 17:29
     * @title addBirthdayCouponConfig
     * @param storeId
     * @param couponIds
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 添加新会员配置
     */
    public Resp addNewVipCouponConfig(String storeId,String[] couponIds){
        List<NewVipCouponConfig> newVipCouponConfigs = new ArrayList<>();
        for (String couponId:couponIds){
            Coupon coupon = couponService.getRepository().findByStoreIdAndDelFlagAndId(storeId,1,couponId);
            if(coupon != null){
                NewVipCouponConfig newVipCouponConfig = new NewVipCouponConfig(storeId,couponId);
                newVipCouponConfigs.add(newVipCouponConfig);
            }
        }
        newVipCouponConfigRepository.save(newVipCouponConfigs);
        return new Resp("操作成功!");
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/21 17:35
     * @title deleteBirthdayCouponConfig
     * @param storeId
     * @param newVipCouponConfigId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 删除新会员的某配置
     */
    public Resp deleteNewVipCouponConfig(String storeId,String newVipCouponConfigId){
        NewVipCouponConfig newVipCouponConfig = newVipCouponConfigRepository.findOne(newVipCouponConfigId);
        if(newVipCouponConfig == null){
            return new Resp(Resp.PARAM_ERROR,"数据异常!");
        }
        if(!newVipCouponConfig.getStoreId().equals(storeId)){
            return new Resp(Resp.PARAM_ERROR,"数据异常!");
        }
        newVipCouponConfigRepository.delete(newVipCouponConfigId);
        return new Resp("操作成功!");
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/16 10:30
     * @title storeGetNewVipCoupon
     * @param storeId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 获取商户配置的优惠券
     */
    public Resp storeGetNewVipCoupon(String storeId){
        List<NewVipCouponToStoreVo> newVipCouponToStoreVos = newVipCouponConfigRepository.findNewVipCouponToStoreVo(storeId);
        return new Resp(newVipCouponToStoreVos,"获取商户配置Vip券成功!");
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/16 17:49
     * @title grantNewVipCoupon
     * @param storeId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 查询会员可以领取的券
     */
    public List<NewVipCouponToUserVo> findNewVipCouponToUserVo(String storeId){
        List<NewVipCouponToUserVo> newVipCouponToUserVos  = newVipCouponConfigRepository.findNewVipCouponToUserVo(storeId);
        for(int i = newVipCouponToUserVos.size()-1 ; i >= 0 ;i--){
            if(newVipCouponToUserVos.get(i).getShareType() == 2 && newVipCouponToUserVos.get(i).getShare() == 0){
                newVipCouponToUserVos.remove(i);
            }
        }
        return newVipCouponToUserVos;
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/18 16:35
     * @title receiveNewVipCoupon
     * @param customerVipId
     * @param type
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 领取会员券
     */
    public Resp receiveNewVipCoupon(String customerVipId,Integer type){
        CustomerVip customerVip = customerVipService.findOne(customerVipId);
        if(customerVip == null){
            return new Resp(Resp.PARAM_ERROR,"数据异常!");
        }
        if(!ParamUtil.isBlank(customerVip.getNewVipGetCouponRecord())){
            return new Resp(Resp.PARAM_ERROR,"无法领取,您已经领取过会员券了!");
        }
        if(type == 1) {
            CustomerInfo customerInfo = customerInfoService.findOne(customerVip.getCustomerInfoId());
            if (customerInfo == null) {
                return new Resp(Resp.PARAM_ERROR, "数据异常!");
            }
            List<NewVipCouponToUserVo> newVipCouponToUserVos = findNewVipCouponToUserVo(customerVip.getStoreId());
            for (NewVipCouponToUserVo newVipCouponToUserVo:newVipCouponToUserVos){
                couponService.customerVipGetCoupon(customerVipId,newVipCouponToUserVo.getCouponId(),false);
            }
        }
        customerVip.setNewVipGetCouponRecord(CustomTimeUtil.getSysYear());
        customerVipService.save(customerVip);
        return new Resp("操作成功!");
    }
}
